<?php

	class Country extends Model{

		private $_CountryId;
		private $_Name;
		private $_Flag;
		private $_Continent;
		private $_Active;

		function __construct($arrInitialValues = null){
			parent::__construct();

			if (isset($arrInitialValues['country_id']))
				$this->setCountryId($arrInitialValues['country_id']);
			else
				$this->setCountryId(0);

			if (isset($arrInitialValues['name']))
				$this->setName($arrInitialValues['name']);
			else
				$this->setName('');

			if (isset($arrInitialValues['flag']))
				$this->setFlag($arrInitialValues['flag']);
			else
				$this->setFlag('');

			if (isset($arrInitialValues['continent']))
				$this->setContinent($arrInitialValues['continent']);
			else
				$this->setContinent('');

			if (isset($arrInitialValues['active']))
				$this->setActive($arrInitialValues['active']);
			else
				$this->setActive(0);
		}
		
		public function setCountryId($value){
			$this->_CountryId = $value;
		}
		
		public function getCountryId(){
			return $this->_CountryId;
		}
		
		public function setName($value){
			$this->_Name = $value;
		}
		
		public function getName(){
			return $this->_Name;
		}
		
		public function setFlag($value){
			$this->_Flag = $value;
		}
		
		public function getFlag(){
			return $this->_Flag;
		}
		
		public function setContinent($value){
			$this->_Continent = $value;
		}
		
		public function getContinent(){
			return $this->_Continent;
		}
		
		public function setActive($value){
			$this->_Active = $value;
		}
		
		public function getActive(){
			return $this->_Active;
		}
		
		public static function getAll($active = 0){
			$query = '
				SELECT countries.country_id, countries.name, countries.flag, continents.name as continent, countries.active
				FROM countries
				INNER JOIN continents 
					ON countries.continent = continents.continent_id ';
			if ($active)
				$query .= ' WHERE active = 1 ';
			$query .= '
				ORDER BY countries.name';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public static function getAllActive(){
			return Country::getAll(1);
		}
		
		public function getCountry(){
			$query = '
				SELECT *
				FROM countries
				WHERE country_id = ' . $this->_CountryId;
			$dbHelper = new DBHelper($query);

			return $dbHelper->getResult();
		}

		public function deleteFlag(){
			@unlink(MAIN_IMAGES_FLAG_DIRECTORY . $this->_Flag);
		}

		public function update(){
			$query = '
				UPDATE countries
				SET name = "' . $this->_Name . '", 
				continent = "' . $this->_Continent . '", 
				active = ' . $this->_Active . ' 
				WHERE country_id = ' . $this->_CountryId;

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public function updateFlag(){
			$query= '
				UPDATE countries
				SET flag = "' . $this->_Flag . '"
				WHERE country_id = ' . $this->_CountryId;

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		private function changeStatus(){
			$query='
				UPDATE countries
				SET active = ' . $this->getActive() . '
				WHERE country_id= ' . $this->getCountryId();
			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function activate(){
			$this->setActive(1);
			return $this->changeStatus();
		}
		
		public function delete(){
			$this->setActive(0);
			return $this->changeStatus();
		}
		
		public function create(){
			$query = '
				INSERT INTO countries
				(name, flag, continent, active)
				VALUES
				("' . $this->getName() . '", "' . $this->getFlag() . '", "' . $this->getContinent() . '", ' . $this->getActive() . ')';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
	}
?>
